﻿using System;
using System.Web;
using System.Drawing;
using Microsoft.SharePoint;
using System.IO;
using SPThumbnailExtender.Interne;
using SPThumbnailExtender.Handler;

namespace SPThumbnailExtender
{
    public class MobileWmvHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/png";

            string strQWmv = context.Request.QueryString["wmv"];
            string strWidth = context.Request.QueryString["width"];
            string strHeight = context.Request.QueryString["height"];
            string strCount = context.Request.QueryString["count"];
            string strPage = context.Request.QueryString["n"];
            int? _imgWidth = null;
            int? _imgHeight = null;
            int? _iPage = null;
            bool? _bCount = null;
            bool? _bCache = null;

            int imgWidth;
            if (int.TryParse(strWidth, out imgWidth))
            {
                _imgWidth = imgWidth;
            }

            int imgHeight;
            if (int.TryParse(strHeight, out imgHeight))
            {
                _imgHeight = imgHeight;
            }

            int iPage;
            if (int.TryParse(strPage, out iPage))
            {
                _iPage = iPage;
            }
            bool bCache;
            if (bool.TryParse(context.Request["usecache"], out bCache))
            {
                _bCache = bCache;
            }


            bool bCount;
            if (bool.TryParse(strCount, out bCount))
            {
                _bCount = bCount;
            }
            HandlerHelper.ProcessRequest(context, strQWmv, _imgWidth, _imgHeight, _iPage, _bCount, _bCache, typeof(ThumbnailWMV));
        }
        #endregion
    }
}
